package com.naiterui.ehp.bp.security.controller;

import com.naiterui.ehp.bp.security.annotation.AnonymousDeleteMapping;
import com.naiterui.ehp.bp.security.annotation.AnonymousGetMapping;
import com.naiterui.ehp.bp.security.annotation.AnonymousPostMapping;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.security.jwt.TokenProvider;
import com.naiterui.ehp.bp.security.service.IAuthorizationService;
import com.naiterui.ehp.bp.security.service.IOnlineUserService;
import com.naiterui.ehp.bp.security.utils.BaseJwtUser;
import com.naiterui.ehp.bp.security.utils.SecurityUtils;
import com.naiterui.ehp.bp.security.vo.AuthorizationFaceInfoVO;
import com.naiterui.ehp.bp.security.vo.AuthorizationInfoVO;
import com.naiterui.ehp.bp.security.vo.AuthorizationSmsVO;
import com.naiterui.ehp.bp.security.vo.AuthorizationUserVO;
import com.naiterui.ehp.bp.security.vo.VerificationCodeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;

/**
 * 授权、根据token获取用户详细信息
 */
@Slf4j
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
@Api(tags = "系统：系统授权接口")
public class AuthorizationController {

  private final TokenProvider tokenProvider;
  private final IOnlineUserService onlineUserService;
  private final IAuthorizationService authorizationService;

  @SysLog("获取登录验证码")
  @ApiOperation("获取登录验证码")
  @AnonymousPostMapping(value = "/smscode")
  public AuthorizationSmsVO smscode(@Validated @RequestBody AuthorizationUserVO authUser) {
    return this.authorizationService.smsCode(authUser);
  }

  @SysLog("用户登录")
  @ApiOperation("登录授权")
  @AnonymousPostMapping(value = "/login")
  public AuthorizationInfoVO login(@Validated @RequestBody AuthorizationUserVO authUser) {
    return this.authorizationService.login(authUser);
  }

  @SysLog("PC人脸登录查询")
  @ApiOperation("登录授权")
  @AnonymousPostMapping(value = "/login/{username}/{faceId}")
  public AuthorizationFaceInfoVO login(@Validated @PathVariable("faceId") @NotEmpty(message = "人脸标识不能为空") String faceId,
                                       @Validated @PathVariable("username") @NotEmpty(message = "用户名不能为空") String username) {
    return this.authorizationService.faceLogin(faceId, username);
  }

  @ApiOperation("获取用户信息")
  @GetMapping(value = "/info")
  public BaseJwtUser getUserInfo() {
    return SecurityUtils.getCurrentUser();
  }

  @ApiOperation("获取验证码")
  @AnonymousGetMapping(value = "/code")
  public VerificationCodeVO getCode() {
    return this.authorizationService.getCode();
  }

  @ApiOperation("退出登录")
  @AnonymousDeleteMapping(value = "/logout")
  public void logout(HttpServletRequest request) {
    this.onlineUserService.logout(this.tokenProvider.resolveToken(request));
  }

}
